package com.travel.controller;

import cn.hutool.core.bean.BeanUtil;
import com.travel.dto.AdminUserLoginDTO;
import com.travel.pojo.AdminUser;
import com.travel.result.Result;
import com.travel.service.AdminUserService;
import com.travel.service.impl.TokenService;
import com.travel.vo.AdminUserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: libin
 * Date: 2025/6/10 15:25
 * Description: 后台人员
 **/
@RestController
@RequestMapping("/travel/admin/user")
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;

    @Autowired
    private TokenService tokenService;

    @PostMapping("/login")
    public Result<AdminUserLoginVO> login(@RequestBody AdminUserLoginDTO adminUserLoginDTO) {
        AdminUser adminUser = adminUserService.login(adminUserLoginDTO);
        AdminUserLoginVO adminUserLoginVO = BeanUtil.copyProperties(adminUser, AdminUserLoginVO.class);
        //生成JWT令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", adminUser.getId());
        claims.put("userName", adminUser.getUserName());
        String token = tokenService.createToken(claims);
        adminUserLoginVO.setToken(token);
        return Result.success(adminUserLoginVO);
    }
}
